package com.mywebsite.Controller;


import com.mywebsite.Domain.EmailCode;
import com.mywebsite.Domain.User;
import com.mywebsite.Service.EmailService;
import com.mywebsite.Service.Impl.UserServiceImpl;
import com.mywebsite.Service.UserService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.util.Map;

//统一通过JSON格式传输数据

@RestController
@RequestMapping("v1/users")
@CrossOrigin
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    EmailService emailService;

    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> userinfo){
        //判断非法用户名
        String username = userinfo.get("username");
        String password = userinfo.get("password");
        if (username == "" || username == null){
            return new Result(Code.Error,"请输入正确用户名!");
        }
        //判断不存在用户
        User userCheck = userService.getByName(username);
        if ( userCheck == null ){
                return new Result(Code.Error,"用户不存在，请先点击下方注册!");
        }
        //判断密码
        String pwd = userCheck.getPassword();
        if(!(pwd.equals(password))){
             return new Result(Code.Error,"密码错误请重试!");
         }
        return new Result(Code.Success,"登录成功!");
    }

    @PostMapping("/register")
    public Result register(@RequestBody Map<String, String> userinfo){
        //用户名验证必须是4-10位(字母、数字、下划线)
        String regUserName = "^[^0-9][\\w_]{3,9}$";
        //密码验证必须是6-20位(字母、数字、下划线)
        String regPwd = "^[\\w_]{6,20}$";

        String username = userinfo.get("username");
        String password = userinfo.get("password");
        String useremail = userinfo.get("useremail");
        String verificationCode = userinfo.get("verificationCode");
        if (username == "" || password == "" || useremail == "" || verificationCode == ""){
            return new Result(Code.Error,"请补全注册信息!");
        }

        User user = userService.getByName(username);
        EmailCode emailCode = new EmailCode();
        emailCode.setVfCode(verificationCode);
        emailCode.setUserEamil(useremail);

        if (!username.matches(regUserName)){
            return new Result(Code.Error,"用户名格式为4-10位字母,数字,下划线任意组合!");
        }
        else if (!password.matches(regPwd)) {
            return new Result(Code.Error, "密码格式为6-20位字母,数字,下划线任意组合!");
        }
        else if(user != null){
            return new Result(Code.Error, "该用户名已被注册!");
        }
        else if(!emailService.checkEmail(emailCode)){
            return new Result(Code.Error, "验证码错误!");
        }
        userService.register(username,password,useremail);
        return new Result(Code.Success, "注册成功!");
    }


    @PostMapping("/getVfcode")
    public Result vfCode(@RequestBody Map<String, String>useremail) throws MessagingException {
        String code = emailService.sendEmail(useremail.get("useremail"));
        if (code == "" || code == null || code == "Error"){
            return new Result(Code.Error, "验证码发送失败!");
        }
        return new Result(Code.Success, "验证码发送成功,5分钟之内有效!");
    }

    @PostMapping("/update/baseinfo")
    public Result updateBaseinfo(@RequestBody Map<String, String> userinfos){
        //用户名验证必须是4-10位(字母、数字、下划线)
        String regUserName = "^[^0-9][\\w_]{3,9}$";
        String username  =userinfos.get("username");
        String newusername = userinfos.get("newusername");
        String userinfo  = userinfos.get("userinfo");
        if (username == "" || username == null){
            return new Result(Code.Error, "修改错误,请检查登录状态");
        }
        if (newusername == "" || newusername == null){
            newusername = username;
            userService.update(username,newusername,"","",userinfo);
            return new Result(Code.Success,"更新用户信息成功");
        }
        User user = userService.getByName(newusername);
        if (user != null){
            return new Result(Code.Error,"用户名重复请更换一个!");
        }
        if (!newusername.matches(regUserName)){
            return new Result(Code.Error,"用户名格式为4-10位字母,数字,下划线任意组合!");
        }
        userService.update(username,newusername,"","",userinfo);
        return new Result(Code.Success,"更新用户信息成功");
    }
    @PostMapping("/update/password")
    public Result updatePassword(@RequestBody Map<String, String> userinfos){
        String regPwd = "^[\\w_]{6,20}$";
        String username  =userinfos.get("username");
        String password = userinfos.get("password");
        String useremail = userinfos.get("useremail");
        String verificationCode = userinfos.get("verificationCode");
        EmailCode emailCode = new EmailCode();
        emailCode.setVfCode(verificationCode);
        emailCode.setUserEamil(useremail);

        if (username == "" || username == null){
            return new Result(Code.Error, "修改错误,请检查登录状态");
        }
        if (password == "" || password == null){
            return new Result(Code.Error, "修改错误,请正确填写信息");
        }else if (!password.matches(regPwd)) {
            return new Result(Code.Error, "密码格式为6-20位字母,数字,下划线任意组合!");
        }else if(!emailService.checkEmail(emailCode)){
            return new Result(Code.Error, "验证码错误!");
        }

        userService.update(username,"",password,"","");


        return new Result(Code.Error,"更新密码成功");
    }

    @PostMapping("/update/useremail")
    public Result updateUseremail(@RequestBody Map<String, String> userinfos){
        String username  =userinfos.get("username");
        String useremail = userinfos.get("useremail");
        String newuseremail = userinfos.get("newuseremail");
        String verificationCode = userinfos.get("verificationCode");
        EmailCode emailCode = new EmailCode();
        emailCode.setVfCode(verificationCode);
        emailCode.setUserEamil(useremail);
        if (username == "" || username == null){
            return new Result(Code.Error, "修改错误,请检查登录状态");
        }
        else if(!emailService.checkEmail(emailCode)){
            return new Result(Code.Error, "验证码错误!");
        }
        userService.update(username,"","",newuseremail,"");
        return new Result(Code.Success, "邮箱更新成功");
    }
}
